<template>
  <div class="slider" ref="slider">
    <div class="slider-group" ref="sliderGroup">
      <slot>
      </slot>
    </div>
     <ul class="name-group">
        <li class="img-name"  v-show="currentPageIndex+1 === index" v-for="(item,index) in goodsList">{{item.name}}</li>
    </ul>
    <div class="dots">
      <span class="dot" :class="{active: currentPageIndex+1 === index }" v-for="(item, index) in dots"></span>
    </div>
  </div>
</template>
<script type="text/javascript">
import BScroll from 'better-scroll'
export default {
  name: 'slider',
  props: ['goodsList'],
  data () {
    return {
      dots: [],
      currentPageIndex: 0,
      loop: true,
      autoPlay: true,
      interval: 3000
    }
  },
  mounted () {
    setTimeout(() => {
      this._setSliderWidth(true)
      this._initDots()
      this._initSlider()
      if (this.autoPlay) {
        this._play()
      }
    }, 20)

    window.addEventListener('resize', () => {
      if (!this.slider) {
        return
      }
      this._setSliderWidth(false)
      this.slider.refresh()
    })
  },
  activated () {
    if (this.autoPlay) {
        this._play()
      }
    },
  beforeDestroy () {
    clearTimeout(this.timer)
  },
  methods: {
    _setSliderWidth (isResize) {
      this.children = this.$refs.sliderGroup.children
      var width = 0
      let sliderWidth = this.$refs.slider.clientWidth
      for (let i = 0; i < this.children.length; i++) {
        let child = this.children[i]
        child.style.width = sliderWidth + 'px'
        width += sliderWidth
      }
      if (this.loop && isResize) {
        width += 2 * sliderWidth
        console.log(width)
      }
      this.$refs.sliderGroup.style.width = width + 'px'
    },
    // 初始化滚动
    _initSlider () {
      this.slider = new BScroll(this.$refs.slider, {
        scrollX: true,
        eventPassthrough: 'vertical',
        momentum: false,
        snap: {
          loop: true,
          threshold: 0.3,
          speed: 400
        }
      })
      this.slider.on('scrollEnd', () => {
        let pageIndex = this.slider.getCurrentPage().pageX
        
        if (true) {
          pageIndex -= 1
        }
        this.currentPageIndex = pageIndex
        if (this.autoPlay) {
          clearTimeout(this.timer)
          this._play()
        }
      })
    },
    // dot点的循环显示
    _initDots () {
      this.dots = new Array(this.children.length)
    },
    _play () {
      let pageIndex = this.currentPageIndex +1
      // console.log(`currentIndex: ${this.currentPageIndex }`)
      if (true) {
        pageIndex = pageIndex + 1
      }
      if (pageIndex === this.children.length-2) {
        pageIndex = 0
        this.currentIndex = 0         
      }
      // console.log(`pageIndex:${pageIndex}`)
      this.timer = setTimeout(() => {
        this.slider.goToPage(pageIndex, 0, 400)
      }, 2000)
    }
  }
}
</script>
<style lang="less">
.slider {
  position: relative;
  min-height: 1px;
  overflow: hidden;
  width:3.333333rem;
  height: 5.2rem;
  margin: 0 auto;
  .slider-group {
    position: relative;
    overflow: hidden;
    white-space: nowrap;
  }
  .dots {
    position: absolute;
    right: 0;
    left: 0;
    bottom: 0;
    text-align: center;
    font-size: 0;
    .dot {
      display: inline-block;
      margin: 0 8px;
      width: 0.16rem;
      height: 0.16rem;
      border-radius: 50%;
      background: rgb(240,195,195);
      &.active {
        background: rgb(260,55,55);
      }
    }
  }
}

</style>